package com.lysj.bhp.wallet.util;

import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.json.JacksonUtil;
import com.lysj.bhp.util.web.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

public class EtherScanUtil {
    private final static String API_SERVICE_URL = "https://api.etherscan.io/";
    private final static String API_KEY = "153CMRRMWA972994U61AIF72H2EEV2JVXV";

    private final static String BHPC_CONTRACT_ADDRESS = "0xee74110fb5a1007b06282e0de5d73a61bf41d9cd";


    private static Logger logger = LoggerFactory.getLogger(EtherScanUtil.class);


    /**
     * 查询以太坊地址余额
     */
    public static BigInteger getBalance(String address) {
        address = "0x" + address;
//        address = "0x4b749518b313f553316e86303efb78dd92dfdc36";

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("module", "account");
        paramMap.put("action", "balance");
        paramMap.put("address", address);
        paramMap.put("tag", "latest");
        paramMap.put("apikey", API_KEY);
        String resp = HttpUtil.sendGet(API_SERVICE_URL + "api", HttpUtil.getParamStringFromMap(paramMap));
        if (ParamUtil.isBlack(resp)) {
            return new BigInteger("-1");
        }
        Map<String, String> respMap = JacksonUtil.toStringMap(resp);
        if (respMap == null || respMap.size() == 0) {
            return new BigInteger("-1");
        }
        if (!"1".equalsIgnoreCase(respMap.get("status"))) {
            return new BigInteger("-1");
        }
        return new BigInteger(respMap.get("result"));
    }

    /**
     * 查询以太坊地址的代币余额
     * https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0x57d90b64a1a57749b0f932f1a3395792e12e7055&address=0xe04f27eb70e025b78871a2ad7eabe85e61212761&tag=latest&apikey=YourApiKeyToken
     */
    public static double getBalanceTokenBHPC(String address) {
//        address = "0xe50c2c8f49b72c92074f83164311e7e5120aa3ed";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("module", "account");
        paramMap.put("action", "tokenbalance");
        paramMap.put("contractaddress", BHPC_CONTRACT_ADDRESS);
        paramMap.put("address", address);
        paramMap.put("tag", "latest");
        paramMap.put("apikey", API_KEY);
        String resp = HttpUtil.sendGet(API_SERVICE_URL + "/api", HttpUtil.getParamStringFromMap(paramMap));
        if (ParamUtil.isBlack(resp)) {
            logger.error("Interface return empty string");
            return -1;
        }
        Map<String, String> respMap = JacksonUtil.toStringMap(resp);
        if (respMap == null || respMap.size() == 0) {
            logger.error("Json2Map error -> " + resp);
            return -1;
        }
        if (!"1".equalsIgnoreCase(respMap.get("status"))) {
            logger.error("Interface return error status  -> " + resp);
            return -1;
        }
        logger.info("查询结果 -> " + respMap.get("result"));
        return weiToEther(respMap.get("result"));
    }


    /**
     * Converts the provided Wei amount (smallest value Unit) to Ethers.
     */
    public static double weiToEther(String wei) {
        BigDecimal number = new BigDecimal(wei);
        BigDecimal result = number.divide(BigDecimal.TEN.pow(18));
        return result.doubleValue();
    }

}
